<template>
  <div id="app">
    <h1 class="secret-number" :style="{ color: secretNumberColor }" :class="{ 'blur': !isGameOver }">{{ secretNumber }}</h1>
    <p class="countdown">剩余时间: {{ countdownTime }} 秒</p>
    <p>我已经生成了一个{{ minRange }}到{{ maxRange }}之间的随机数字，快来猜一猜吧！</p>
    <div>
      <input type="number" v-model="guess" placeholder="请输入你猜测的数字" />
      <button @click="checkGuess">提交猜测</button>
    </div>
    <div v-if="resultMessage" class="result-message" v-html="resultMessage"></div>
    <div class="guess-count">
      你已经猜测了 {{ guessCount }} 次
    </div>
    <button v-if="isGameOver" @click="restartGame">重新开始游戏</button>
    
    <!-- 设置按钮 -->
    <button class="settings-btn" @click="toggleSettings">设置</button>
    
    <!-- 设置面板 -->
    <div v-if="showSettings" class="settings-panel">
      <div class="settings-header">
        <h3>游戏设置</h3>
        <button class="close-btn" @click="toggleSettings">×</button>
      </div>
      <div class="settings-content">
        <div>
          <label for="minRange">最小值:</label>
          <input type="number" id="minRange" v-model.number="tempMinRange" min="1" max="{{ tempMaxRange - 1 }}" />
        </div>
        <div>
          <label for="maxRange">最大值:</label>
          <input type="number" id="maxRange" v-model.number="tempMaxRange" min="{{ tempMinRange + 1 }}" />
        </div>
        <div>
          <label for="numberColor">随机数颜色:</label>
          <input type="color" id="numberColor" v-model="tempSecretNumberColor" />
        </div>
      </div>
      <div class="settings-footer">
        <button class="save-btn" @click="saveSettings">保存设置</button>
      </div>
    </div>
    
    <!-- 遮罩层 -->
    <div v-if="showSettings" class="overlay" @click="toggleSettings"></div>
    
    <!-- 雪花效果 -->
    <div class="snowfall">
      <div class="snowflake" v-for="i in 50" :key="i"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      secretNumber: 0,
      guess: null,
      guessCount: 0,
      resultMessage: '',
      isGameOver: false,
      minRange: 1,
      maxRange: 100,
      showSettings: false,
      tempMinRange: 1,
      tempMaxRange: 100,
      tempSecretNumberColor: '#4CAF50', // 临时颜色变量
      lastGeneratedNumbers: [], // 存储最近五分钟内生成的数字
      secretNumberColor: '#4CAF50', // 默认绿色
      cleanInterval: null, // 定期清理历史记录的定时器
      countdownTime: 60, // 倒计时时间（秒）
      countdownInterval: null // 倒计时定时器
    }
  },
  mounted() {
    // 初始加载时生成随机数和随机颜色
    this.generateSecretNumber()
    this.secretNumberColor = this.getRandomGreenColor()
    this.startCountdown()
    // 每10秒清理一次超过五分钟的数字
    this.cleanInterval = setInterval(() => this.cleanOldNumbers(), 10000)
  },
  beforeUnmount() {
    // 组件卸载时清除定时器
    clearInterval(this.cleanInterval)
    clearInterval(this.countdownInterval)
  },
  methods: {
    generateSecretNumber() {
      let newNumber
      const now = Date.now()
      
      // 清理超过五分钟的数字
      this.cleanOldNumbers()
      
      // 生成不重复的数字
      do {
        newNumber = Math.floor(Math.random() * (this.maxRange - this.minRange + 1)) + this.minRange
      } while (this.lastGeneratedNumbers.some(item => item.number === newNumber))
      
      // 保存新生成的数字和时间戳
      this.lastGeneratedNumbers.push({ number: newNumber, timestamp: now })
      this.secretNumber = newNumber
    },
    startCountdown() {
      // 清除之前的定时器
      clearInterval(this.countdownInterval)
      this.countdownTime = 60
      
      // 开始新的定时器
      this.countdownInterval = setInterval(() => {
        this.countdownTime--
        
        // 时间到，游戏结束
        if (this.countdownTime <= 0) {
          this.gameOver('您猜想的时间太长，请重试。')
        }
      }, 1000)
    },
    resetCountdown() {
      this.startCountdown()
    },
    gameOver(message) {
      this.resultMessage = message || '游戏结束'
      this.isGameOver = true
      clearInterval(this.countdownInterval)
    },
    cleanOldNumbers() {
      const fiveMinutesAgo = Date.now() - 5 * 60 * 1000
      this.lastGeneratedNumbers = this.lastGeneratedNumbers.filter(item => item.timestamp >= fiveMinutesAgo)
    },
    getRandomGreenColor() {
      const greenShades = [
        '#4CAF50', '#45a049', '#3e8e41', '#367c39', '#2e6c31',
        '#275a29', '#214a21', '#1a3a19', '#132a11', '#0d1a09'
      ]
      return greenShades[Math.floor(Math.random() * greenShades.length)]
    },
    checkGuess() {
      if (this.isGameOver) return
      
      if (this.guess === null || this.guess < this.minRange || this.guess > this.maxRange) {
        this.resultMessage = `请输入${this.minRange}到${this.maxRange}之间的有效数字！`
        return
      }
      
      this.guessCount++
      
      if (this.guess < this.secretNumber) {
        this.resultMessage = '<span style="color: red; font-size: 60px;">❌</span> 你猜测的数字太小了！'
      } else if (this.guess > this.secretNumber) {
        this.resultMessage = '<span style="color: red; font-size: 60px;">❌</span> 你猜测的数字太大了！'
      } else {
        this.gameOver(`<span style="color: green; font-size: 60px;">✅</span> 恭喜你，猜对了！你一共猜测了${this.guessCount}次。`)
      }
      
      this.guess = null
    },
    restartGame() {
      this.generateSecretNumber()
      this.guess = null
      this.guessCount = 0
      this.resultMessage = ''
      this.isGameOver = false
      this.resetCountdown()
    },
    toggleSettings() {
      this.showSettings = !this.showSettings
      // 打开设置面板时，初始化临时变量
      if (this.showSettings) {
        this.tempMinRange = this.minRange
        this.tempMaxRange = this.maxRange
        this.tempSecretNumberColor = this.secretNumberColor
      }
    },
    saveSettings() {
      // 确保最小值小于最大值
      if (this.tempMinRange >= this.tempMaxRange) {
        this.resultMessage = '最小值必须小于最大值！'
        return
      }
      
      // 更新实际的区间值
      this.minRange = this.tempMinRange
      this.maxRange = this.tempMaxRange
      this.secretNumberColor = this.tempSecretNumberColor
      
      // 关闭设置面板
      this.showSettings = false
      
      // 重新开始游戏
      this.restartGame()
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #ffffff;
  margin-top: 60px;
  position: relative;
  overflow: hidden;
  background-color: #000000;
}

input {
  padding: 8px;
  font-size: 16px;
  margin-right: 10px;
}

button {
  padding: 8px 16px;
  font-size: 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.result-message {
  margin-top: 20px;
  font-size: 48px;
  font-weight: bold;
  padding: 10px;
  border-radius: 4px;
  display: inline-block;
}

.countdown {
  font-size: 20px;
  font-weight: bold;
  color: #e74c3c;
  margin-bottom: 10px;
}

.secret-number {
  font-size: 80px;
  font-weight: bold;
  color: #4CAF50;
  padding: 20px 40px;
  border-radius: 10px;
  margin-bottom: 20px;
  display: inline-block;
  z-index: 1;
  position: relative;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.guess-count {
  margin-top: 10px;
  font-size: 16px;
}

/* 设置按钮样式 */
.settings-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #2c3e50;
  color: white;
  padding: 10px 20px;
  border-radius: 50px;
  font-size: 16px;
  z-index: 1000;
}

.settings-btn:hover {
  background-color: #34495e;
}

/* 设置面板样式 */
.settings-panel {
  position: fixed;
  bottom: 80px;
  right: 20px;
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  z-index: 1001;
  width: 300px;
  text-align: left;
  color: black;
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.settings-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: black;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.close-btn:hover {
  color: #666;
  background-color: #f5f5f5;
  border-radius: 50%;
}

.settings-content div {
  margin-bottom: 15px;
}

.settings-content label {
  display: inline-block;
  width: 80px;
  margin-right: 10px;
  font-weight: bold;
}

.settings-content input {
  width: 180px;
  padding: 6px;
}

/* 颜色选择器样式 */
.settings-content input[type="color"] {
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* 保存按钮样式 */
.save-btn {
  color: black;
  background-color: #ffffff;
  border: 1px solid #cccccc;
}

.save-btn:hover {
  background-color: #f5f5f5;
}

.settings-footer {
  text-align: right;
  border-top: 1px solid #eee;
  padding-top: 15px;
}

/* 遮罩层样式 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

.blur {
  filter: blur(10px);
  -webkit-filter: blur(10px);
}

/* 雪花效果样式 */
.snowfall {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

.snowflake {
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: #ffffff;
  border-radius: 50%;
  animation: snowfall linear infinite;
}

@keyframes snowfall {
  0% {
    transform: translateY(-100px);
    opacity: 0.7;
  }
  100% {
    transform: translateY(calc(100vh + 100px));
    opacity: 0;
  }
}

/* 为每个雪花设置不同的动画延迟和持续时间 */
.snowflake:nth-child(1) {
  left: 10%;
  animation-duration: 15s;
  animation-delay: 0s;
}

.snowflake:nth-child(2) {
  left: 20%;
  animation-duration: 20s;
  animation-delay: 5s;
}

.snowflake:nth-child(3) {
  left: 30%;
  animation-duration: 18s;
  animation-delay: 3s;
}

.snowflake:nth-child(4) {
  left: 40%;
  animation-duration: 25s;
  animation-delay: 7s;
}

.snowflake:nth-child(5) {
  left: 50%;
  animation-duration: 22s;
  animation-delay: 2s;
}

.snowflake:nth-child(6) {
  left: 60%;
  animation-duration: 17s;
  animation-delay: 9s;
}

.snowflake:nth-child(7) {
  left: 70%;
  animation-duration: 24s;
  animation-delay: 1s;
}

.snowflake:nth-child(8) {
  left: 80%;
  animation-duration: 19s;
  animation-delay: 6s;
}

.snowflake:nth-child(9) {
  left: 90%;
  animation-duration: 21s;
  animation-delay: 4s;
}

.snowflake:nth-child(10) {
  left: 5%;
  animation-duration: 23s;
  animation-delay: 8s;
}

/* 更多雪花样式... */
.snowflake:nth-child(11) { left: 15%; animation-duration: 16s; animation-delay: 10s; }
.snowflake:nth-child(12) { left: 25%; animation-duration: 20s; animation-delay: 12s; }
.snowflake:nth-child(13) { left: 35%; animation-duration: 18s; animation-delay: 14s; }
.snowflake:nth-child(14) { left: 45%; animation-duration: 22s; animation-delay: 16s; }
.snowflake:nth-child(15) { left: 55%; animation-duration: 19s; animation-delay: 18s; }
.snowflake:nth-child(16) { left: 65%; animation-duration: 24s; animation-delay: 20s; }
.snowflake:nth-child(17) { left: 75%; animation-duration: 17s; animation-delay: 22s; }
.snowflake:nth-child(18) { left: 85%; animation-duration: 21s; animation-delay: 24s; }
.snowflake:nth-child(19) { left: 95%; animation-duration: 23s; animation-delay: 26s; }
.snowflake:nth-child(20) { left: 2%; animation-duration: 15s; animation-delay: 28s; }

.snowflake:nth-child(21) { left: 12%; animation-duration: 20s; animation-delay: 30s; }
.snowflake:nth-child(22) { left: 22%; animation-duration: 18s; animation-delay: 32s; }
.snowflake:nth-child(23) { left: 32%; animation-duration: 25s; animation-delay: 34s; }
.snowflake:nth-child(24) { left: 42%; animation-duration: 22s; animation-delay: 36s; }
.snowflake:nth-child(25) { left: 52%; animation-duration: 17s; animation-delay: 38s; }

.snowflake:nth-child(26) { left: 62%; animation-duration: 24s; animation-delay: 40s; }
.snowflake:nth-child(27) { left: 72%; animation-duration: 19s; animation-delay: 42s; }
.snowflake:nth-child(28) { left: 82%; animation-duration: 21s; animation-delay: 44s; }
.snowflake:nth-child(29) { left: 92%; animation-duration: 23s; animation-delay: 46s; }
.snowflake:nth-child(30) { left: 7%; animation-duration: 16s; animation-delay: 48s; }

.snowflake:nth-child(31) { left: 17%; animation-duration: 20s; animation-delay: 50s; }
.snowflake:nth-child(32) { left: 27%; animation-duration: 18s; animation-delay: 52s; }
.snowflake:nth-child(33) { left: 37%; animation-duration: 22s; animation-delay: 54s; }
.snowflake:nth-child(34) { left: 47%; animation-duration: 19s; animation-delay: 56s; }
.snowflake:nth-child(35) { left: 57%; animation-duration: 24s; animation-delay: 58s; }

.snowflake:nth-child(36) { left: 67%; animation-duration: 17s; animation-delay: 60s; }
.snowflake:nth-child(37) { left: 77%; animation-duration: 21s; animation-delay: 62s; }
.snowflake:nth-child(38) { left: 87%; animation-duration: 23s; animation-delay: 64s; }
.snowflake:nth-child(39) { left: 97%; animation-duration: 15s; animation-delay: 66s; }
.snowflake:nth-child(40) { left: 4%; animation-duration: 20s; animation-delay: 68s; }

.snowflake:nth-child(41) { left: 14%; animation-duration: 18s; animation-delay: 70s; }
.snowflake:nth-child(42) { left: 24%; animation-duration: 25s; animation-delay: 72s; }
.snowflake:nth-child(43) { left: 34%; animation-duration: 22s; animation-delay: 74s; }
.snowflake:nth-child(44) { left: 44%; animation-duration: 17s; animation-delay: 76s; }
.snowflake:nth-child(45) { left: 54%; animation-duration: 24s; animation-delay: 78s; }

.snowflake:nth-child(46) { left: 64%; animation-duration: 19s; animation-delay: 80s; }
.snowflake:nth-child(47) { left: 74%; animation-duration: 21s; animation-delay: 82s; }
.snowflake:nth-child(48) { left: 84%; animation-duration: 23s; animation-delay: 84s; }
.snowflake:nth-child(49) { left: 94%; animation-duration: 16s; animation-delay: 86s; }
.snowflake:nth-child(50) { left: 8%; animation-duration: 20s; animation-delay: 88s; }
</style>
